package com.hy.study.reduce;

import com.hy.study.bean.SortFlowBean;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 02 月 07 日 16:01: 35
 */
public class TopNReducer extends Reducer<SortFlowBean, Text, Text, NullWritable> {
    private final Text k = new Text();

    @Override
    protected void reduce(SortFlowBean key, Iterable<Text> values, Reducer<SortFlowBean, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        StringBuilder builder = new StringBuilder();
        for (Text value : values) {
            builder.append(value).append("\t");
        }
        builder.append(key.getUpFlow()).append("\t").append(key.getDownFlow()).append("\t").append(key.getSumFlow());
        k.set(builder.toString());
        context.write(k, NullWritable.get());
    }
}
